package com.warm.orm.listener;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.annotation.UpdateListener;
import com.warm.auth.helper.LoginHelper;
import com.warm.core.constant.HttpStatus;
import com.warm.core.exception.ServiceException;
import com.warm.orm.domain.BaseEntity;

import java.util.Date;

/**
 * 全局修改监听器
 *
 * @author warm
 */
public class HhUpdateListener implements UpdateListener {

    @Override
    public void onUpdate(Object object) {
        try {
            if (ObjectUtil.isNotNull(object)) {
                BaseEntity entity = (BaseEntity) object;
                entity.setUpdateBy(LoginHelper.getUsernameNe());
                entity.setUpdateTime(new Date());
            }
        } catch (Exception e) {
            throw new ServiceException("修改自动注入异常 => " + e.getMessage(), HttpStatus.UNAUTHORIZED);
        }
    }
}
